﻿using System;
using System.Collections.Specialized;
using System.Linq;
using System.Web.Security;
using Castle.MonoRail.Framework;
using StateTheaterDataModel;
using StateTheater.Models;
using Castle.Core;

namespace StateTheater.Controllers
{
	[Layout("default"), Rescue("generalerror")]
	public class ActorWatchController : SmartDispatcherController
	{
		StateTheaterDataContext db;
		public ActorWatchController(StateTheaterDataContext db)
		{
			this.db = db;
		}

		public void Add(int id)
		{
			MembershipUser user = Membership.GetUser();
			if (user == null || user.ProviderUserKey == null)
			{
				this.CancelView();
				this.RenderView("Description");
				return;
			}
			int memberId = (int)user.ProviderUserKey;
			ActorWatch AW = new ActorWatch()
			{
				PersonID = id,
				MEMBER_ID = memberId
			};

			db.ActorWatches.InsertOnSubmit(AW);
			db.SubmitChanges();

		}

		public void Remove(int id)
		{
			var result = RemoveDirect(id);

			PropertyBag["DeletedName"] = result.First;
			Edit();
			this.CancelView();
			this.RenderView("Edit");
		}

		[Rescue(typeof(JsonRescueController))]
		[return: JSONReturnBinder]
		public Pair<string, bool> RemoveDirect(int id)
		{
			try
			{
				ActorWatch AW = db.ActorWatches.Single(aw => aw.PK == id);
				var name = AW.People.FirstName + " " + AW.People.LastName;
				db.ActorWatches.DeleteOnSubmit(AW);
				db.SubmitChanges();
				return new Pair<string, bool>(name, true);
			}
			catch
			{
				return new Pair<string, bool>("", false);
			}
		}

		public void Remove(string code)
		{
			NameValueCollection codes = Util.DecodeBlock(code);
			int pk = Convert.ToInt32(codes["pk"]);
			Remove(pk);
		}

		public void RemoveAll()
		{
		}

		public void Edit()
		{
			//select 	'<TR><TD>' + P.FirstName + ' ' + p.LastName +'</TD>' + 
			//        '<TD><input type="BUTTON" value="Delete" onClick="DeleteIt('+cast(AW.PK as varchar)+')" ></TD></TR>'
			//from 	People p, 
			//    ActorWatch AW
			//where  AW.Member_id = @memberid
			//and 	AW.PersonID = p.PersonID
			//order by p.LastName, p.FirstName

			MembershipUser user = Membership.GetUser();
			int memberId = (int)user.ProviderUserKey;

			PropertyBag["watches"]= 
				(from aw in db.ActorWatches
					where aw.MEMBER_ID ==  memberId
					orderby aw.People.LastName, aw.People.FirstName
					select new 
					{
						aw.People.FirstName,
						aw.People.LastName,
						ID = aw.PK
					}).ToList();
		}

		public void Description()
		{
		}

		public void Display()
		{
			var user = Membership.GetUser();
			if (user != null)
			{
				int memberId = (int)user.ProviderUserKey;
				var start = DateTime.Now.AddDays(6);
				var enddate = DateTime.Now.AddDays(-1600);

				var actors = (
				from a in db.ActorWatches
				join r in db.Roles on a.PersonID equals r.PersonID
				where a.MEMBER_ID == memberId
				 && r.Production.FirstPerformance < start
				 && r.Production.LastPerformance > enddate
				// select new { r.Production.Play.Title, r.People.FullName}
				select new
				{
					PK = a.PK,
					Person = new NameDetails
					{
						FirstName = r.People.FirstName,
						LastName = r.People.LastName,
						ID = r.PersonID,
						HasBio = r.People.HasBio,
						HasPhoto=r.People.HasPhoto
					},
					r.Production.Play.Title,
					r.ProductionID,
					r.Production.TroupeID,
					TroupeName = Util.AdjTitle(r.Production.Troupe.Name),
					r.Production.Venue.City,

					FirstPerformance= r.Production.FirstPerformance.ToShortDateString(),
//					LastPerformance = r.Production.LastPerformance
				}).ToList();

				foreach (var a in actors) { a.Person.SEOName = Util.SEOEscape(a.Person.FullName, false); }
				PropertyBag["actors"] = actors;

				var crew = (
				from a in db.ActorWatches
				join r in db.ProductionCredits on a.PersonID equals r.PersonID
				where a.MEMBER_ID == memberId
				 && r.Production.FirstPerformance < start
				 && r.Production.LastPerformance > enddate
				select new
				{
					PK = a.PK,
					Person = new NameDetails
					{
						FirstName = r.People.FirstName,
						LastName = r.People.LastName,
						ID = r.PersonID.Value,
						HasBio = r.People.HasBio,
						HasPhoto=r.People.HasPhoto
					},
					r.Production.Play.Title,
					r.ProductionID,
					r.Production.TroupeID,
					TroupeName = Util.AdjTitle(r.Production.Troupe.Name),
					r.Production.Venue.City,
					Credit = r.CreditName.Credit,
					FirstPerformance= r.Production.FirstPerformance.ToShortDateString(),
//					LastPerformance = r.Production.LastPerformance
				}).ToList();
				foreach (var a in crew) { a.Person.SEOName = Util.SEOEscape(a.Person.FullName, false); }
				PropertyBag["crew"] = crew;


				var writers = (
				from a in db.ActorWatches
				join r in db.PlayCredits on a.PersonID equals r.PersonID
				join p in db.Productions on r.PlayID equals p.PlayID
				where a.MEMBER_ID == memberId
				 && p.FirstPerformance < start
				 && p.LastPerformance > enddate
				// select new { r.Production.Play.Title, r.People.FullName}
				select new
				{
					PK = a.PK,
					Person = new NameDetails
					{
						FirstName = r.People.FirstName,
						LastName = r.People.LastName,
						ID = r.PersonID.Value,
						HasBio = r.People.HasBio,
						HasPhoto = r.People.HasPhoto
					},
					p.Play.Title,
					p.ProductionID,
					Credit = r.CreditName.Credit,
					p.TroupeID,
					TroupeName = Util.AdjTitle(p.Troupe.Name),
					p.Venue.City,

					FirstPerformance = p.FirstPerformance.ToShortDateString(),
					//					LastPerformance = r.Production.LastPerformance
				}).ToList();
				foreach (var a in writers) { a.Person.SEOName = Util.SEOEscape(a.Person.FullName, false); }
				PropertyBag["writers"] = writers;




			}
		}
	}
}
